package com.invengo.library_orionstar;

import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.ainirobot.coreservice.client.module.ModuleCallbackApi;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;

import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;

public class ModuleCallback extends ModuleCallbackApi {

    private static final String TAG = ModuleCallback.class.getName();

    /**
     * Receive speech request
     * 接收语音指令 底层发起request 请求
     *
     * @param reqId
     * @param reqType  语音指令类型, Speech type
     * @param reqText  语音识别内容, Speech text
     * @param reqParam 语音指令参数, Speech param
     * @throws RemoteException
     */
    @Override
    public boolean onSendRequest(int reqId, String reqType, String reqText, String reqParam) throws RemoteException {
        Log.d(TAG, "New request: " + " type is:" + reqType + " text is:" + reqText + " reqParam = " + reqParam);
        String[] intentDoman = reqType.split("&");
        String Intent = intentDoman[0];
        String domain = intentDoman[1];
        switch (Intent) {
            case "opeartion":
            case "general_command_3":
                handlerOpeartion(domain);
                break;
            case "general_command":
                if (domain.equals("stop")) {
                    RobotManager.getInstance().stopTTS();
                } else {
                    handlerOpeartion(domain);
                }
//                Messenger.getDefault().send(SpeechEvent.answer(answer), "SPEECH_TO_PAGE");
                break;
            case "weather":
                Messenger.getDefault().send(SpeechEvent.answer(getAnswer(reqParam)), "SPEECH_TO_PAGE");
                break;
            case "invengo_library":
                handlerDomain(domain);
                break;
            case "open_app":
                handlerLaunch(reqParam);
                break;
            default:
                handlerAnswer(reqParam);
                break;
        }
        return true;
    }

    private void handlerLaunch(String reqParam) {
        try {
            String path = "";
            JSONArray jsonArray = JSON.parseObject(reqParam).getJSONObject("skillData").getJSONObject("open_app").getJSONArray("params");
            List<PathBean> beans = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<PathBean>>() {
            }.getType());
            if (beans != null && !TextUtils.isEmpty(beans.get(0).getValue()))
                if (!TextUtils.isEmpty(beans.get(0).getValue())) {
                    path = beans.get(0).getValue();
                }
            Messenger.getDefault().send(SpeechEvent.answer("好的，为您跳转！"), "SPEECH_TO_PAGE");
            Messenger.getDefault().send(path, "LAUNCH_PAGE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void handlerAnswer(String reqParam) {
        String answer = getAnswer(reqParam);
        if (RobotManager.getInstance().isSearch()) {
            KLog.e("搜索界面");
            Messenger.getDefault().send(SpeechEvent.startSearch(), "SPEECH_TO_PAGE");
        } else {
            KLog.e("非搜索界面");
            if (!TextUtils.isEmpty(answer)) {
                KLog.e("去回答");
                Messenger.getDefault().send(SpeechEvent.answer(answer), "SPEECH_TO_PAGE");
            } else {
                KLog.e("去搜索");
                Messenger.getDefault().send(SpeechEvent.startSearch(), "SPEECH_TO_PAGE");
//                Messenger.getDefault().send(SpeechEvent.answer("我听不懂你在说什么"), "SPEECH_TO_PAGE");
            }
        }
    }

    private String getAnswer(String reqParam) {
        JSONObject jsonObject = JSON.parseObject(reqParam);
        return jsonObject.getString("answerText");
    }

    private void handlerDomain(String domain) {
        Messenger.getDefault().send(SpeechEvent.answer("好的，为您跳转！"), "SPEECH_TO_PAGE");
        switch (domain) {
            case "search_book":
                Messenger.getDefault().send("/robot/Search", "LAUNCH_PAGE");
                break;
            case "search_popular":
                Messenger.getDefault().send("/robot/Popular", "LAUNCH_PAGE");
                break;
            case "search_activity":
                Messenger.getDefault().send("/robot/Advert", "LAUNCH_PAGE");
                break;
        }
    }

    private void handlerOpeartion(String domain) {
        switch (domain) {
            case "return_home":
            case "home":
            case "stop":
                KLog.e("xxx");
                Messenger.getDefault().send(ActionEvent.opHome(), "OPEARTION_TO_PAGE");
                break;
            case "search":
                Messenger.getDefault().send(ActionEvent.opSearch(), "OPEARTION_TO_PAGE");
                break;
            case "info":
                Messenger.getDefault().send(ActionEvent.opInfo(), "OPEARTION_TO_PAGE");
                break;
            case "back":
                Messenger.getDefault().send(ActionEvent.opBack(), "OPEARTION_TO_PAGE");
                break;
            case "down":
            case "next":
                Messenger.getDefault().send(ActionEvent.opDown(), "OPEARTION_TO_PAGE");
                break;
            case "up":
            case "return":
                Messenger.getDefault().send(ActionEvent.opUp(), "OPEARTION_TO_PAGE");
                break;
            case "top":
                Messenger.getDefault().send(ActionEvent.opMoveTop(), "OPEARTION_TO_PAGE");
                break;
            case "buttom":
                Messenger.getDefault().send(ActionEvent.opMoveButtom(), "OPEARTION_TO_PAGE");
                break;
            case "change":
                Messenger.getDefault().send(ActionEvent.opChange(), "OPEARTION_TO_PAGE");
                break;
        }
    }

    /**
     * hardware error callback
     * 硬件出现异常回调
     *
     * @param function
     * @param type
     * @param message
     * @throws RemoteException
     */
    @Override
    public void onHWReport(int function, String type, String message) throws RemoteException {
        Log.i(TAG, "onHWReport function:" + function + " type:" + type + " message:" + message);
        Log.i(TAG, "onHWReport function:" + function + " type:" + type + " message:" + message);
    }

    /**
     * Suspend system, after this message, RobotOS can not work with this APP
     * 控制权被系统剥夺，收到该事件后，所有Api调用无效
     *
     * @throws RemoteException
     */
    @Override
    public void onSuspend() throws RemoteException {
        KLog.e("控制权被系统剥夺，收到该事件后，所有Api调用无效");
    }

    /**
     * Recovery system, after this message, RobotOS can work with this APP again.
     * 控制权恢复，收到该事件后，重新恢复对机器人的控制
     *
     * @throws RemoteException
     */
    @Override
    public void onRecovery() throws RemoteException {
        KLog.e("控制权恢复，收到该事件后，重新恢复对机器人的控制");
    }
}
